<?php
class StatisAction extends BaseAction{
	// 首页
	public function index(){
		/**用来处理带有条件的查询**/
		$partner_id = $_SESSION['qp_id'];
		
		$totalsell = service('Statistics')->countGoodsSellByPid($partner_id);
		$this->assign('totalsell', $totalsell);
		
		//初始化请求参数
		$statmodel = $_REQUEST['statmodel'];//统计查询类型 shanghu-按商户统计 goods-按商品统计
		$checkerid = $_REQUEST['checkerid'];//对应商户ID
		$gid = $_REQUEST['gid'];//对应商品ID
		$stime = $_REQUEST['stime'];//开始时间
		$etime = $_REQUEST['etime'];//结束时间
		$timeexp = $_REQUEST['timeexp'];//按月或日统计
		//dump($_REQUEST['gid']);
		
		//初始化参数
		$isbelongp = D('Partner','Admin')->isBelongPartner($checkerid,$_SESSION['qp_parent_id']);
		if(!$isbelongp)$checkerid = null;//$partner_id;
		//if(!empty($stime))$stime = paramDate($stime);
		//if(!empty($etime))$etime = paramDate($etime);
		if(!in_array($statmodel,array('shanghu','goods')))$statmodel='shanghu';
		
		$this->assign('statmodel', $statmodel);
		$this->assign('checkerid', $checkerid);
		$this->assign('gid', $gid);
		$this->assign('stime', $stime);
		$this->assign('etime', $etime);
		$this->assign('timeexp', $timeexp);
		$this->display();
	}
	
	//统计查询方法
	public function ajxstatis(){
		Log::record('ajxstatis->请求参数：'.(parseAry2Str($_REQUEST)),Log::DEBUG,Log::FILE);
		
		$statmodel = $_REQUEST['statmodel'];//统计查询类型 shanghu-按商户统计 goods-按商品统计
		$checkerid = $_REQUEST['checkerid'];//对应要统计的商户ID
		$gid = $_REQUEST['gid'];//对应商品ID
		$stime = $_REQUEST['stime'];//开始时间
		$etime = $_REQUEST['etime'];//结束时间
		$timeexp = $_REQUEST['timeexp'];//按月或日统计
		//$isbelongp = D('Partner','Admin')->isBelongPartner($pid,$_SESSION['qp_parent_id']);
		//if(!$isbelongp)
		$map = array('pid'=>$_SESSION['qp_id'],'gid'=>$gid,'rtntype'=>'chart');
		//初始化参数
		if(!empty($stime))$stime = paramDate($stime);
		if(!empty($etime))$etime = paramDate($etime);
		
		if(empty($stime)&&empty($etime)){
			$stattype=null;
		}else{
			$stattype='time';
		}
		$isbelongp = D('Partner','Admin')->isBelongPartner($checkerid,$_SESSION['qp_parent_id']);
		if(!$isbelongp)$checkerid = null;//$partner_id;
		if($checkerid!=null){
			$map['checker'][]	= $checkerid;
		}else{
			$pdata = D('Partner','Admin')->selectPartnerInfo(array('porppid'=>$_SESSION['qp_id'],'noroot'=>'yes'));
			$map['checker'] = getSubByKey($pdata,"id");
		}
		$map['stattype'] 	= $stattype;
		$map['timeInterval']['etime'] = $etime;
		$map['timeInterval']['stime'] = $stime;
		
		if(in_array($timeexp,array('day','month'))){
			$map['timeexp']=$timeexp;
		}
		Log::record('ajxstatis->查询条件：'.$map['pid'].(parseAry2Str($map)),Log::DEBUG,Log::FILE);

		//按照统计方式进行调用
		if(!in_array($statmodel,array('shanghu','goods')))$statmodel='shanghu';
		if($statmodel=='shanghu'){
			$statdata = service('Statistics')->statGoods4Checked($map,$_REQUEST['order'],true,4);
		}
		if($statmodel=='goods'){
			$statdata = service('Statistics')->statPartner4Checked($map,$_REQUEST['order'],true,4);
		}
		
		Log::record('ajxstatis->查询结果：'.(parseAry2Str($statdata)),Log::DEBUG,Log::FILE);
		Log::save(); 
		//Log::record('ajxstatis->查询条件：'.$map['pid'].(parseAry2Str($map)),Log::DEBUG,Log::FILE);
		exit(json_encode( array('status'=>'1','data'=>$statdata) ));
	}
	
	public function ajxstatgoods(){
		$pid = $_SESSION['qp_parent_id'];
		$gid = $_REQUEST['gid'];
		$stattype = $_REQUEST['stattype'];
		//echo $gid."|".$stattype;
		Log::record('ajxstatsell 当前parentid='.$pid,Log::DEBUG,Log::FILE);
		Log::save(); 
		if(empty($pid))$pid='-';
		$pdata = D('Partner','Admin')->selectPartnerInfo(array('ppid'=>$_SESSION['qp_id']));
		$checker = getSubByKey($pdata,"id");
		$statdata = service('Statistics')->statGoods4Checked(array('pid'=>$pid,'gid'=>$gid,'rtntype'=>'chart','stattype'=>$stattype,'checker'=>$checker),'down',true,8);
		
		exit(json_encode( array('status'=>'1','data'=>$statdata) ));
	}
	
	public function ajxstatshangh(){
		
		
		$pid = $_SESSION['qp_parent_id'];
		Log::record('ajxstatsell 当前parentid='.$pid,Log::DEBUG,Log::FILE);
		Log::save(); 
		if(empty($pid))$pid='-';
		$pdata = D('Partner','Admin')->selectPartnerInfo(array('ppid'=>$_SESSION['qp_id']));
		$checker = getSubByKey($pdata,"id");
		$statdata = service('Statistics')->statPartner4Checked(array('pid'=>$pid,'rtntype'=>'chart','stattype'=>'time','checker'=>$checker),'down',true,6);
		//dump($statdata);
		exit(json_encode( array('status'=>'1','data'=>$statdata) ));
	}
}

?>